package com.kgcx.organization.service;

import com.github.pagehelper.PageHelper;
import com.kgcx.common.bean.RequestData;
import com.kgcx.common.constant.Cons;
import com.kgcx.dbutils.domain.BaseDomain;
import com.kgcx.dbutils.utils.PageUtils;
import com.kgcx.organization.domain.Supplier;
import com.kgcx.organization.mapper.SupplierMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;

    public Supplier getSupplier(Long kid) {
        return supplierMapper.selectByPrimaryKey(kid);
    }

    public List<Supplier> getSupplierList(RequestData rd) {
        PageUtils.page(rd);
        PageHelper.orderBy(rd.getOrderBy());
        return supplierMapper.selectActive(BaseDomain.buildSelectDomain(rd, new Supplier()));
    }

    @Transactional(rollbackFor = Exception.class)
    public void addSupplier(Supplier supplier) {
        supplierMapper.insertSelective(supplier);
    }

    @Transactional(rollbackFor = Exception.class)
    public void updateSupplier(Supplier supplier) {
        supplierMapper.updateByPrimaryKeySelective(supplier);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeSupplier(Long kid) {
        Supplier supplier = new Supplier();
        supplier.setKid(kid);
        supplier.setStatus(Cons.COMMON_STATUS_DELETE);
        supplierMapper.updateByPrimaryKeySelective(supplier);
    }
}
